$(() => {
    function getWeather(city, cityID, picID, num) {
        let result;
        let result1;
        let high;
        let low;
        let type;
        let fengxiang;
        let xmlhttp;
        if (window.XMLHttpRequest) {
            //  IE7+, Firefox, Chrome, Opera, Safari 浏览器执行代码
            xmlhttp = new XMLHttpRequest();
        } else {
            // IE6, IE5 浏览器执行代码
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        //建立连接
        xmlhttp.open("GET", "http://wthrcdn.etouch.cn/weather_mini?city=" + city, true);
        //发送请求
        xmlhttp.send();
        //响应
        xmlhttp.onreadystatechange = function () {
            if (xmlhttp.readyState === 4 && xmlhttp.status === 200) {
                let obj = JSON.parse(xmlhttp.responseText);     //  将json数据转换成对象
                let smg = obj.data.forecast;
                high = smg[0].high;
                low = smg[0].low;
                type = smg[0].type;
                fengxiang = smg[0].fengxiang;
                // let a = high.split(" ")[1];
                // result = type + " " + high + " " + low + " " + fengxiang;
                result = low.split(" ")[1] + " ~ " + high.split(" ")[1] + " ";
                result1 = fengxiang + " " + type;
                document.getElementById(cityID).innerText = result;
                document.getElementById(cityID).nextElementSibling.innerText = result1;
                // console.log(smg[3].date);    //  大后天天气
                //  添加天气图例
                if (num === 1) {
                    switch (type) {
                        case "暴雪":
                            document.getElementById(picID).style.backgroundImage = "url('images/weather(50px)/xue.png')";
                            break;
                        case "暴雨":
                            document.getElementById(picID).style.backgroundImage = "url('images/weather(50px)/yu.png')";
                            break;
                        case "大暴雨":
                            document.getElementById(picID).style.backgroundImage = "url('images/weather(50px)/xue.png')";
                            break;
                        case "大雪":
                            document.getElementById(picID).style.backgroundImage = "url('images/weather(50px)/xue.png')";
                            break;
                        case "大雨":
                            document.getElementById(picID).style.backgroundImage = "url('images/weather(50px)/yu.png')";
                            break;
                        case "冻雨":
                            document.getElementById(picID).style.backgroundImage = "url('images/weather(50px)/yu.png')";
                            break;
                        case "多云":
                            document.getElementById(picID).style.backgroundImage = "url('images/weather(50px)/duoyun.png')";
                            break;
                        case "浮尘":
                            document.getElementById(picID).style.backgroundImage = "url('images/weather(50px)/duoyun.png')";
                            break;
                        case "雷阵雨":
                            document.getElementById(picID).style.backgroundImage = "url('images/weather(50px)/yu.png')";
                            break;
                        case "雷阵雨伴有冰雹":
                            document.getElementById(picID).style.backgroundImage = "url('images/weather(50px)/leizhenyu.png')";
                            break;
                        case "霾":
                            document.getElementById(picID).style.backgroundImage = "url('images/weather(50px)/duoyun.png')";
                            break;
                        case "强沙尘暴":
                            document.getElementById(picID).style.backgroundImage = "url('images/weather(50px)/duoyun.png')";
                            break;
                        case "晴":
                            document.getElementById(picID).style.backgroundImage = "url('images/weather(50px)/qing.png')";
                            break;
                        case "沙尘暴":
                            document.getElementById(picID).style.backgroundImage = "url('images/weather(50px)/duoyun.png')";
                            break;
                        case "特大暴雨":
                            document.getElementById(picID).style.backgroundImage = "url('images/weather(50px)/yu.png')";
                            break;
                        case "雾":
                            document.getElementById(picID).style.backgroundImage = "url('images/weather(50px)/duoyun.png')";
                            break;
                        case "小雪":
                            document.getElementById(picID).style.backgroundImage = "url('images/weather(50px)/xue.png')";
                            break;
                        case "小雨":
                            document.getElementById(picID).style.backgroundImage = "url('images/weather(50px)/yu.png')";
                            break;
                        case "扬沙":
                            document.getElementById(picID).style.backgroundImage = "url('images/weather(50px)/duoyun.png')";
                            break;
                        case "阴":
                            document.getElementById(picID).style.backgroundImage = "url('images/weather(50px)/yin.png')";
                            break;
                        case "雨夹雪":
                            document.getElementById(picID).style.backgroundImage = "url('images/weather(50px)/xue.png')";
                            break;
                        case "阵雪":
                            document.getElementById(picID).style.backgroundImage = "url('images/weather(50px)/xue.png')";
                            break;
                        case "阵雨":
                            document.getElementById(picID).style.backgroundImage = "url('images/weather(50px)/yu.png')";
                            break;
                        case "中雪":
                            document.getElementById(picID).style.backgroundImage = "url('images/weather(50px)/xue.png')";
                            break;
                        case "中雨":
                            document.getElementById(picID).style.backgroundImage = "url('images/weather(50px)/yu.png')";
                            break;
                        default:
                            break;
                    }
                } else if (num === 2) {
                    switch (type) {
                        case "暴雪":
                            document.getElementById(picID).style.backgroundImage = "url('images/weather(100px)/xue.png')";
                            break;
                        case "暴雨":
                            document.getElementById(picID).style.backgroundImage = "url('images/weather(100px)/yu.png')";
                            break;
                        case "大暴雨":
                            document.getElementById(picID).style.backgroundImage = "url('images/weather(100px)/xue.png')";
                            break;
                        case "大雪":
                            document.getElementById(picID).style.backgroundImage = "url('images/weather(100px)/xue.png')";
                            break;
                        case "大雨":
                            document.getElementById(picID).style.backgroundImage = "url('images/weather(100px)/yu.png')";
                            break;
                        case "冻雨":
                            document.getElementById(picID).style.backgroundImage = "url('images/weather(100px)/yu.png')";
                            break;
                        case "多云":
                            document.getElementById(picID).style.backgroundImage = "url('images/weather(100px)/duoyun.png')";
                            break;
                        case "浮尘":
                            document.getElementById(picID).style.backgroundImage = "url('images/weather(100px)/duoyun.png')";
                            break;
                        case "雷阵雨":
                            document.getElementById(picID).style.backgroundImage = "url('images/weather(100px)/yu.png')";
                            break;
                        case "雷阵雨伴有冰雹":
                            document.getElementById(picID).style.backgroundImage = "url('images/weather(100px)/leizhenyu.png')";
                            break;
                        case "霾":
                            document.getElementById(picID).style.backgroundImage = "url('images/weather(100px)/duoyun.png')";
                            break;
                        case "强沙尘暴":
                            document.getElementById(picID).style.backgroundImage = "url('images/weather(100px)/duoyun.png')";
                            break;
                        case "晴":
                            document.getElementById(picID).style.backgroundImage = "url('images/weather(100px)/qing.png')";
                            break;
                        case "沙尘暴":
                            document.getElementById(picID).style.backgroundImage = "url('images/weather(100px)/duoyun.png')";
                            break;
                        case "特大暴雨":
                            document.getElementById(picID).style.backgroundImage = "url('images/weather(100px)/yu.png')";
                            break;
                        case "雾":
                            document.getElementById(picID).style.backgroundImage = "url('images/weather(100px)/duoyun.png')";
                            break;
                        case "小雪":
                            document.getElementById(picID).style.backgroundImage = "url('images/weather(100px)/xue.png')";
                            break;
                        case "小雨":
                            document.getElementById(picID).style.backgroundImage = "url('images/weather(100px)/yu.png')";
                            break;
                        case "扬沙":
                            document.getElementById(picID).style.backgroundImage = "url('images/weather(100px)/duoyun.png')";
                            break;
                        case "阴":
                            document.getElementById(picID).style.backgroundImage = "url('images/weather(100px)/yin.png')";
                            break;
                        case "雨夹雪":
                            document.getElementById(picID).style.backgroundImage = "url('images/weather(100px)/xue.png')";
                            break;
                        case "阵雪":
                            document.getElementById(picID).style.backgroundImage = "url('images/weather(100px)/xue.png')";
                            break;
                        case "阵雨":
                            document.getElementById(picID).style.backgroundImage = "url('images/weather(100px)/yu.png')";
                            break;
                        case "中雪":
                            document.getElementById(picID).style.backgroundImage = "url('images/weather(100px)/xue.png')";
                            break;
                        case "中雨":
                            document.getElementById(picID).style.backgroundImage = "url('images/weather(100px)/yu.png')";
                            break;
                        default:
                            break;
                    }
                }

            }
        };
    }


    getWeather("长春", "weather_cc", "pic_cc", 1);
    getWeather("大连", "weather_dl", "pic_dl", 1);
    getWeather("石家庄", "weather_sjz", "pic_sjz", 1);
    getWeather("包头", "weather_bt", "pic_bt", 1);
    getWeather("刚察", "weather_qhh", "pic_qhh", 1);
    getWeather("南通", "weather_nt", "pic_nt", 1);
    getWeather("广州", "weather_gz", "pic_gz", 1);
    getWeather("南宁", "weather_nn", "pic_nn", 1);
    getWeather("呼伦贝尔", "weather_hlbe", "pic_hlbe", 1);
    getWeather("敦煌", "weather_dh", "pic_dh", 1);

    getWeather("长春", "centerWeather_cc", "centerPic_cc", 2);
    getWeather("大连", "centerWeather_dl", "centerPic_dl", 2);
    getWeather("石家庄", "centerWeather_sjz", "centerPic_sjz", 2);
    getWeather("包头", "centerWeather_bt", "centerPic_bt", 2);
    getWeather("刚察", "centerWeather_qhh", "centerPic_qhh", 2);
    getWeather("南通", "centerWeather_nt", "centerPic_nt", 2);
    getWeather("广州", "centerWeather_gz", "centerPic_gz", 2);
    getWeather("南宁", "centerWeather_nn", "centerPic_nn", 2);
    getWeather("呼伦贝尔", "centerWeather_hlbe", "centerPic_hlbe", 2);
    getWeather("敦煌", "centerWeather_dh", "centerPic_dh", 2);

});
